<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class PurController extends Controller
{
    public function xiaomi5splus(Request $request)
    {
        $datas = DB::table("comments")->get();
        $reply = DB::table("reply")->get();
        // dd($data);
        // foreach($a as $v){
        //     $data  = $v;
        // }
        // var_dump($data);
        $goodsid =$request->input('goodsid');
        // dd($goodsid);
        if ($request->session()->has('homeuser')) {
            $id = $request->session()->get('homeuser')->userid;
            $data = DB::table('user') 
                        ->where('userid','=',$id)
                        ->crossJoin('goods')
                        ->where('goodsid','=',$goodsid)
                        ->select('goodsid','userid','subtitle','picture','goodsname','path','price','preferential','stock')
                        ->get();
                        $a = $data[0];
                        // 把对象转为数组
                        $b = [];
                        foreach ($a as $k => $v) {
                            $b[$k] = $v;
                        }
                        $aa = array_filter($b);
            //  把foot表中用户id和商品id相同的数据删除再插入
            DB::table('foot')->where([['userid','=',$id],['goodsid','=',$goodsid]])->delete(); 
            DB::table('foot')->insert($aa);
            // $foot = DB::table('foot')->where([['userid','=',$id],['goodsid','=',$goodsid]])->get();
            //     if ($foot->all() == []) {
            //         DB::table('foot')->insert($aa);
            //     }        
            $list = DB::table('goods')
                    ->where('goodsid','=',$goodsid)
                    ->join('goodsdet','goodsdetid','=','goodsid')
                    ->select('goodsdet.*','goodsname','picture')
                    ->get()
                    ->all();
            // dd($list);
            if (!$list == []) {
                 return view('home.xiaomi5splus.purxiaomi5splus',['list'=>$list,'userid'=>$id,'datas'=>$datas,'reply'=>$reply]);
            }else{
                return redirect('/purxiaomi5splus?goodsid=7');
            }
        }else{
            // dd($goodsid);
            return view('home.xiaomi5splus.purlogin',['goodsid'=>$goodsid]);
        }
    }
    // 处理立即登录的登陆页面
    public function purlogin(Request $request)
    {
        // dd($request->input('goodsid'));
        $name = $request->input("user");
        $goodsid = $request->input('goodsid');
        // $pass = $request->input("pwd");
        $ob = DB::table("user")->where("username",$name)->first();
        if(empty($name)){
            return back()->with("msg","请输入账号");
        }
        if($ob){
            if($request->input("pwd")==$ob->userpassword){
                session(["homeuser"=>$ob]);
                // 登陆成功进入购物车页
                return redirect("/home/purxiaomi5splus?goodsid="."$goodsid");
            }else{
                return back()->with("msg","登录失败：密码错误");
            }
        }else{
            return back()->with("msg","登录失败：账号不存在");
        }
    }
}
